/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.node;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;

import cgl.narada.protocol.Protocol;

/** 
    This class helps us setup multiple client nodes within one process.
    Each of the these client node's thus setup, can maintain connections
    to different server nodes within the network. In addition to this, 
    they can also have specific subscription predicates. 
    This class is very useful especially in the testing scenario where we have
    multiple clients being attached to multiple server nodes within the server
    network. The sockets are assigned assigned automatically to the client.
    
    @author Shrideep Pallickara
    @version $Revision$
    $Date$ 
*/

public class MultipleClientNode implements NodeDebugFlags {
  Vector clientNodeVec;
  int clientPortNum;
  
  /** The multiple client node constructor */
  public MultipleClientNode() {
    clientNodeVec = new Vector();
  }
  
  /** This sets the initial portNum for a muli-client node, individual
      clients listen to sockets at portNums (which are incremented by 1
      for every new client created) 
      @param portNum The starting port number
  */
  public void setClientPortNum(int portNum) {
    clientPortNum = portNum;
  }
  
  /**
     This method sets up multiple client nodes with a specific subscription
     to a given node.
     @param serverName The name of the machine the server node is hosted on.
     @param portNum The port number that the server node listens to for 
     connections either from clients or servers.
     @param numOfNodes The number of client nodes that must have a connection
     to the server.
     @param subscription The subscription predicate for the client nodes 
     created by this invocation 
  */
  public void createMultipleClientNodesToServer(String serverName,
						int portNum,
						int numOfNodes,
						String subscription) {
    int offset = clientNodeVec.size();
    for (int i=0; i<numOfNodes; i++) {
      ClientNode clientNode = new ClientNode();
      clientNode.setUpNode(clientPortNum++, Protocol.TCP);
      clientNode.connectTo(serverName, portNum, Protocol.TCP);
      clientNodeVec.addElement(clientNode);
      try {
	Thread.sleep(500);
      } catch(InterruptedException ie) {
	ie.printStackTrace();
      }
    }
    
    String server = serverName + ":" +portNum;
    for (int i=0; i <numOfNodes; i++) {
      ClientNode clientNode = (ClientNode)clientNodeVec.elementAt(offset+i);
      clientNode.nodeAddressRequest(server);
      
      try {
	Thread.sleep(1000);
      } catch(InterruptedException ie) {
	ie.printStackTrace();
      }
    }
    
    for (int i=0; i <numOfNodes; i++) {
      ClientNode clientNode = (ClientNode)clientNodeVec.elementAt(offset+i);
      clientNode.profilePropagationRequest(server, subscription);
      try {
	Thread.sleep(10);
      } catch(InterruptedException ie) {
	ie.printStackTrace();
      }
    }
    
  }
  
  /** The test fucntion for the multiple client node */
  public static void main(String[] args) {
    if (args.length !=1) {
      System.out.println("Usage:: java mpaces....MultipleClientNode" + 
			 " portNumForClients");
      System.exit(0);
    }

    MultipleClientNode clientNodes = new MultipleClientNode();
    clientNodes.setClientPortNum(Integer.parseInt(args[0]));

    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    String cmd;
    StringTokenizer tok;
    System.out.println("\n" + "Type h for help and Usage indicators" + "\n");
    while (true) {
      try {
	cmd = r.readLine();
	if (cmd.equals("q")) break;
	if (cmd == null) continue;
	
	if(cmd.startsWith("h")) {
	}
	if(cmd.startsWith("mn")) {
	  tok = new StringTokenizer(cmd, " ");
	  if (tok.countTokens() != 5) {
	    System.out.println("Usage -> mn server portNum numOfNodes subscription");
	    continue;
	  }
	  tok.nextToken();
	  String serverName = tok.nextToken();
	  int portNum = Integer.parseInt(tok.nextToken());
	  int numOfNodes = Integer.parseInt(tok.nextToken());
	  clientNodes.createMultipleClientNodesToServer(serverName, portNum, 
							numOfNodes,
							tok.nextToken());
	}
	
      }catch (IOException ioe) {
	ioe.printStackTrace();
      }
    }
    System.out.println("Program Exiting Now");
  }
}
